package co.edu.usbcali.raju.games.chess.model;

import co.edu.usbcali.raju.model.Piece;

public class ChessPiece implements Piece{
	
	public static final String PAWN="pawn";
	public static final String KNIGHT="knight";
	public static final String BISHOP="bishop";
	public static final String QUEEN="queen";
	public static final String ROOK="rook";
	public static final String KING="king";

	public static final String FWPAWN="P";
	public static final String FWKNIGHT="N";
	public static final String FWBISHOP="B";
	public static final String FWQUEEN="Q";
	public static final String FWROOK="R";
	public static final String FWKING="K";

	public static final String FBPAWN="p";
	public static final String FBKNIGHT="n";
	public static final String FBBISHOP="b";
	public static final String FBQUEEN="q";
	public static final String FBROOK="r";
	public static final String FBKING="k";

	private String player;
	private String type;
	private ChessCell cell;
	
	public ChessPiece(String type, String player) {
		super();
		this.player = player;
		this.type = type;
	}

	public ChessPiece(ChessPiece piece,ChessCell cell) {
		super();
		this.player = piece.getPlayer();
		this.type = piece.getType();
		this.cell = cell;
	}

	public String getPlayer() {
		return player;
	}

	public void setPlayer(String player) {
		this.player = player;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public String toString(){
		StringBuffer stringBuffer=new StringBuffer();
		stringBuffer.append(getType().substring(0,2));
		stringBuffer.append(" ");
		stringBuffer.append(getPlayer().substring(0,1));
		stringBuffer.append(" en "+getPosition().getRow()+" "+getPosition().getCol());
		return stringBuffer.toString();
	}

	public ChessCell getCell() {
		return cell;
	}

	public void setCell(ChessCell cell) {
		if(this.cell!=null){
			this.cell.remove(this);
		}
		this.cell = cell;
		this.cell.addPiece(this);
	}
	
	public ChessPosition getPosition(){
		return (ChessPosition)getCell().getPosition();
	}
	
	public int getRow(){
		return getPosition().getRow();
	}
	
	public int getCol(){
		return getPosition().getCol();
	}
	
	public String getFEN(){
		if(this.getPlayer().equals("white")){
			if(this.type.equals(PAWN)){
				return FWPAWN;
			}
			if(this.type.equals(ROOK)){
				return FWROOK;
			}
			if(this.type.equals(KNIGHT)){
				return FWKNIGHT;
			}
			if(this.type.equals(BISHOP)){
				return FWBISHOP;
			}
			if(this.type.equals(QUEEN)){
				return FWQUEEN;
			}
			if(this.type.equals(KING)){
				return FWKING;
			}			
		}else{
			if(this.type.equals(PAWN)){
				return FBPAWN;
			}
			if(this.type.equals(ROOK)){
				return FBROOK;
			}
			if(this.type.equals(KNIGHT)){
				return FBKNIGHT;
			}
			if(this.type.equals(BISHOP)){
				return FBBISHOP;
			}
			if(this.type.equals(QUEEN)){
				return FBQUEEN;
			}
			if(this.type.equals(KING)){
				return FBKING;
			}
		}
		return null;
	}

}
